#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
#include <cmath>
#include <iostream>
#include <fstream>
#include <iostream>
#include <string>
#include <functional>

#include "UI.h"
#include "Rect2.h"

Rect2 rect = Rect2(0,0,15,15);
UI::Button test = UI::Button(rect,"test");

bool colorchange = false;

int windowSizeWidth = 600;
int windowSizeHeight = 600;

int screenSizeWidth = 200;
int screenSizeHeight = 200;


void display(void)
{
	
	

	glClear(GL_COLOR_BUFFER_BIT);
	if (colorchange)
	{
		glColor3f(1.0, 0.0, 0.0);
	}
	else
	{
		glColor3f(0.0, 1.0, 0.0);
	}
	
	glLineWidth(3.0);
	

	glBegin(GL_POLYGON);
		glVertex3f(test.rect.bottom, test.rect.left, 0);
		glVertex3f(test.rect.bottom, test.rect.right, 0);
		glVertex3f(test.rect.top, test.rect.right, 0);
		glVertex3f(test.rect.top, test.rect.left, 0);
	glEnd();
	
	glFlush();

	glutSwapBuffers();

}

void init(void)
{
	glClearColor(1.0, 1.0, 1.0, 0.0);
	glOrtho(-screenSizeWidth/2, screenSizeWidth/2, -screenSizeHeight/2, screenSizeHeight/2, -10.0, 10.0);
	//glOrtho(-500, 500, -500, 500, -10.0, 10.0);
}

class ButtonFunctions 
{
public:
	void testMethod(int a, int b)
	{
		cout << a << " " << b << endl;
	}
};

void myMouse(int button, int state, int x, int y)
{
	if (button == GLUT_LEFT_BUTTON)
	{
		int positonY = (-y + (windowSizeWidth/2)) / (windowSizeWidth/screenSizeWidth);
		int positonX = (x - (windowSizeHeight/2)) / (windowSizeHeight/screenSizeHeight);

		colorchange = test.handleClick(positonX,positonY);
	}

}



int main(int argc, char** argv)
{
	using namespace std::placeholders;
	ButtonFunctions obj;
	test.runFunction = std::bind( &ButtonFunctions::testMethod, obj, _1, _2 );

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(windowSizeWidth, windowSizeHeight);
	glutInitWindowPosition(50, 50);
	glutCreateWindow("Test");
	init();
	glutMouseFunc(myMouse);
	glutDisplayFunc(display);
	glutMainLoop();
	return 0;
}
